<template>
    
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片 -->
    <el-card>
      <el-button
        type="primary"
        style="margin-bottom: 20px"
        @click="dialogVisible = true"
        >添加分类</el-button
      >
      <!-- 表格 -->
      <el-table :data="tableData" style="width: 100%" border>
        <el-table-column label="#" type="index"> </el-table-column>
        <el-table-column label="分类名称" prop="cat_name"> </el-table-column>
        <el-table-column label="是否有效" prop="cat_deleted">
          <template>
            <i class="el-icon-success"></i>
          </template>
        </el-table-column>
        <el-table-column label="排序" prop="cat_level">
          <template>
            <el-button type="primary" size="mini">一级</el-button>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              icon="el-icon-edit"
              type="primary"
              size="mini"
              @click="editList(scope.row.cat_id)"
              >编辑</el-button
            >
            <el-button
              icon="el-icon-delete"
              type="danger"
              size="mini"
              @click="del(scope.row.cat_id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
    <!-- 添加弹框 -->
    <el-dialog title="添加" :visible.sync="dialogVisible" width="50%">
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
      >
        <el-form-item label="分类名称" prop="name">
          <el-input v-model="ruleForm.name"></el-input>
        </el-form-item>
        <div style="margin-left: 30px">
          父级分类
          <el-cascader
            v-model="ruleForm.value"
            :options="options"
            :props="{ expandTrigger: 'hover' }"
          ></el-cascader>
        </div>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogVisible = false"
          >确 定</el-button
        >
      </span>
    </el-dialog>
    <!-- 编辑对话框 -->
    <el-dialog title="收货地址" :visible.sync="eidialogFormVisible">
      <el-form
        :model="editForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="活动名称" prop="cat_name">
          <el-input v-model="editForm.cat_name"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="editAdd">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getList,
  addCate,
  editCate,
  editCater,
  delCater,
} from "../../api/user";
export default {
  name: "",
  data() {
    return {
      tableData: [],
      queryInfo: {
        type: 3,
        pagenum: 1,
        pagesize: 5,
      },
      total: 0,
      dialogVisible: false,
      eidialogFormVisible: false,
      ruleForm: {
        name: "",
        value: "",
      },
      editForm: {
        cat_name: "",
      },
      options: [],
      rules: {
        name: [{ required: true, message: "请输入分类名称", trigger: "blur" }],
        cat_name: [
          { required: true, message: "请输入分类名称", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getdataList();
  },
  computed: {},
  methods: {
    async getdataList(data) {
      const { data: res } = await getList(this.queryInfo);
      console.log(res);
      this.tableData = res.data.result;
      this.total = res.data.total;
    },
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`);
      this.queryInfo.pagesize = val;
      this.getdataList();
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`);
      this.queryInfo.pagenum = val;
      this.getdataList();
    },
    // 添加

    // 编辑
    async editList(id) {
      this.eidialogFormVisible = true;
      const { data: res } = await editCate(id);
      this.editForm = res.data;
      console.log(res);
    },
    // 编辑提交
    async editAdd() {
      const { data: res } = await editCater(this.editForm.cat_id, {
        cat_name: this.editForm.cat_name,
      });
      console.log(res);
      this.eidialogFormVisible = false;
      this.getdataList();
    },
    // 删除
    async del(id){
       this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async() => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
          const {data:res} = await delCater(id)
          console.log(res);
          this.getdataList()
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
    }
  },
};
</script>

<style scoped>
i {
  color: #39ce39;
}
</style>
